package com.drinks.yiniang.ui.fragment.user.password;

import android.content.Context;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.drinks.yiniang.R;
import com.drinks.yiniang.entity.common.StringJson;
import com.drinks.yiniang.io.api.UserApiIO;
import com.drinks.yiniang.ui.activity.user.setting.ForgotPaymentPasswordActivity;
import com.drinks.yiniang.utils.ToastUtils;
import com.hansen.library.Constants;
import com.hansen.library.io.tuple.Tuple2;
import com.hansen.library.listener.APIRequestCallback;
import com.hansen.library.ui.fragment.BaseFragment;
import com.hansen.library.utils.LogUtils;
import com.hansen.library.utils.StringUtils;

/**
 * 验证支付密码
 * A simple {@link Fragment} subclass.
 */
public class VerifyPayPasswordFragment extends BaseFragment {

    private final int REQ_FORGOT_PASSWORD_CODE = 0x01;

    //密码输入框，6位数字
    private EditText et_verify_pay_pwd;
    //错误提示
    private TextView tv_verify_pay_pwd_error_tips;
    //忘记密码
    private TextView tv_verify_pay_pwd_forgot;
    //确认
    private Button btn_verify_pay_pwd_sure;

    private OnPayPasswordVerifyListener onPayPasswordVerifyListener;

    public VerifyPayPasswordFragment() {
        // Required empty public constructor
    }

    public static VerifyPayPasswordFragment newInstance() {
        return new VerifyPayPasswordFragment();
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        try {
            onPayPasswordVerifyListener = (OnPayPasswordVerifyListener) context;
        } catch (ClassCastException e) {
            LogUtils.e(context.toString() + " must implement OnPayPasswordVerifyListener");
        }
    }

    @Override
    protected int getResID() {
        return R.layout.fragment_verify_pay_password;
    }

    @Override
    protected void initView(View mView) {
        et_verify_pay_pwd = mView.findViewById(R.id.et_verify_pay_pwd);

        tv_verify_pay_pwd_error_tips = mView.findViewById(R.id.tv_verify_pay_pwd_error_tips);
        tv_verify_pay_pwd_forgot = mView.findViewById(R.id.tv_verify_pay_pwd_forgot);

        btn_verify_pay_pwd_sure = mView.findViewById(R.id.btn_verify_pay_pwd_sure);
    }

    @Override
    protected void initData() {
        mContext = getActivity();
    }

    @Override
    protected void listener() {
        tv_verify_pay_pwd_forgot.setOnClickListener(this);
        btn_verify_pay_pwd_sure.setOnClickListener(this);
    }

    @Override
    protected void widgetClick(View v) {
        switch (v.getId()) {
            case R.id.tv_verify_pay_pwd_forgot://忘记密码
                startActivityForResult(new Intent(mContext, ForgotPaymentPasswordActivity.class), REQ_FORGOT_PASSWORD_CODE);
                break;
            case R.id.btn_verify_pay_pwd_sure://确认
                doVerifyPassword();
                break;
            default:
                break;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode == Constants.RESULT_CODE_NORMAL && requestCode == REQ_FORGOT_PASSWORD_CODE) {
            if(onPayPasswordVerifyListener != null) onPayPasswordVerifyListener.onPayPasswordVerifyForgot();
        }
    }

    //验证支付密码
    private void doVerifyPassword() {
        String pwd = StringUtils.getEditTextString(et_verify_pay_pwd.getText());

        if (StringUtils.isEmpty(pwd)) {
            //ToastUtils.showShort(R.string.text_hint_input_password);
            tv_verify_pay_pwd_error_tips.setText("*" + getString(R.string.text_hint_input_password));
            return;
        }

        if (pwd.length() != 6) {
            //ToastUtils.showShort(R.string.text_hint_input_six_pay_pwd);
            tv_verify_pay_pwd_error_tips.setText("*" + getString(R.string.text_hint_input_six_pay_pwd));
            return;
        }

        tv_verify_pay_pwd_error_tips.setText("");

        showLoadingDialog();

        UserApiIO.getInstance().doPayPasswordVerify(pwd, new APIRequestCallback<StringJson, Tuple2<Integer, String>>() {
            @Override
            public void onSuccess(StringJson res) {
                if (onPayPasswordVerifyListener != null) {
                    onPayPasswordVerifyListener.onPayPasswordVerifySuccess();
                }
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                ToastUtils.showShort(error._2);

                if (onPayPasswordVerifyListener != null) {
                    onPayPasswordVerifyListener.onPayPasswordVerifyFail();
                }
            }

            @Override
            public void onComplete() {
                dismissDialog();
            }
        });
    }

    public interface OnPayPasswordVerifyListener {
        void onPayPasswordVerifySuccess();
        void onPayPasswordVerifyForgot();
        void onPayPasswordVerifyFail();
    }
}
